package cn.uin.domain.auth.service.user.register.strategy;

import cn.uin.domain.auth.adapter.repository.IAuthRepository;
import cn.uin.domain.auth.model.entity.HomeUserEntity;
import cn.uin.domain.auth.model.entity.HomeUserRegisterEntity;
import cn.uin.domain.auth.model.valobj.RegisterTypeEnum;
import cn.uin.domain.auth.service.user.register.AbstractRegisterService;
import cn.uin.domain.auth.service.user.register.factory.RegisterStrategyFactory;
import cn.uin.types.enums.ResponseCode;
import cn.uin.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;


import javax.annotation.Resource;

/**
 * @ClassName PwdRegisterService
 * @Description:用户名密码注册策略
 * @Author: Administrator
 * @CreateDate: 2025/10/22 10:13
 * @Version: 1.0
 */
@Slf4j
@Service("PwdRegister")
public class PwdRegisterService extends AbstractRegisterService implements ApplicationRunner {

	@Resource
	private IAuthRepository iAuthRepository;

	@Resource
	private PasswordEncoder passwordEncoder;

	@Override
	protected void checkUser(HomeUserRegisterEntity homeUserRegisterEntity) {
		if (StringUtils.isEmpty(homeUserRegisterEntity.getUsername()) ||
				StringUtils.isEmpty(homeUserRegisterEntity.getPassword()) ||
				StringUtils.isEmpty(homeUserRegisterEntity.getPhone()) ||
				StringUtils.isEmpty(homeUserRegisterEntity.getEmail())) {
			throw new AppException(ResponseCode.C0002.getCode(), ResponseCode.C0002.getInfo());
		}
		HomeUserEntity homeUserEntity = iAuthRepository.queryUserByUserName(homeUserRegisterEntity.getUsername());
		if (!ObjectUtils.isEmpty(homeUserEntity)) {
			throw new AppException(ResponseCode.A0001.getCode(), ResponseCode.A0001.getInfo());
		}
		HomeUserEntity homeUserEntityEmail = iAuthRepository.queryUserByUserEmail(homeUserRegisterEntity.getEmail());
		if (!ObjectUtils.isEmpty(homeUserEntityEmail)) {
			throw new AppException(ResponseCode.A0007.getCode(), ResponseCode.A0007.getInfo());
		}
		HomeUserEntity homeUserEntityPhone = iAuthRepository.queryUserByUserPhone(homeUserRegisterEntity.getPhone());
		if (!ObjectUtils.isEmpty(homeUserEntityPhone)) {
			throw new AppException(ResponseCode.A0008.getCode(), ResponseCode.A0008.getInfo());
		}
		// 校验密码合法性(长度大于等于8位
		if (homeUserRegisterEntity.getPassword().length() < 8) {
			throw new AppException(ResponseCode.A0003.getCode(), ResponseCode.A0003.getInfo());
		}
	}

	@Override
	protected HomeUserEntity doRegister(HomeUserRegisterEntity homeUserRegisterEntity) {
		homeUserRegisterEntity.setType(RegisterTypeEnum.PWD.getCode());
		homeUserRegisterEntity.setPassword(passwordEncoder.encode(homeUserRegisterEntity.getPassword()));
		iAuthRepository.saveUser(homeUserRegisterEntity);
		return HomeUserEntity.builder()
				.username(homeUserRegisterEntity.getUsername())
				.email(homeUserRegisterEntity.getEmail())
				.phone(homeUserRegisterEntity.getPhone())
				.build();
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {
		RegisterStrategyFactory.addStrategy("PwdRegister", this);
	}
}
